﻿Public Class FrmPhieuPhat
    Private MaDG As String
    Private MaNV As String = nv.MaNV
    Private MaPhieu As String
    private editingindex as Integer
    Private Sub FrmPhieuPhat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnhuy_Click(sender, e)

    End Sub

    Public Sub ThongTinPhieu(ByVal MaPhieu As String, ByVal MaDG As String, ByVal MaNV As String)
        me.MaPhieu  = MaPhieu
        Me.MaDG = MaDG
        Me.MaNV = MaNV
    End Sub
    Private Sub btntao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntao.Click
        If (btntao.Text = "Tạo &phiếu") Then
            Dim dt As DataTable = clsDocGia.ThongTinDG(txtmadg.Text)
            If ClsPhieuPhat.TonTaiMaPhieu(txtmaphieu.Text) Then
                MsgBox("Nhập mã phiếu khác, mã phiếu này đã tồn tại!", MsgBoxStyle.Information, "Thông tin")
                Exit Sub
            End If
            If dt.Rows.Count <> 1 Then
                MsgBox("Thông tin về độc giả không đúng!", MsgBoxStyle.Information, "Thông tin")
                Exit Sub
            End If
            gbttp.Enabled = False
            gbctp.Enabled = True
            gbbctp.Enabled = True
            btntao.Text = "&Lưu phiếu"
        ElseIf btntao.Text = "&Lưu phiếu" Then
            If dgvpp.RowCount > 0 Then
                ClsPhieuPhat.TaoPhieu(MaPhieu, MaDG, MaNV, Date.Now, txtghichu.Text)
                For Each r As DataGridViewRow In dgvpp.Rows
                    ClsPhieuPhat.ThemChiTiet(txtmaphieu.Text, r.Cells("LyDo").Value, Val(r.Cells("TienPhat")))
                Next
                MsgBox("Tạo phiếu thành công!", MsgBoxStyle.Information, "Thông tin")
                dgvpp.Rows.Clear()
                btntao.Text = "Tạo &mới"
            End If
        ElseIf btntao.Text = "Tạo &mới" Then
            txtghichu.Text = ""
            btnhuy_Click(sender, e)
        End If
    End Sub

    Private Sub btnhuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhuy.Click
        btntao.Text = "Tạo &phiếu"
        gbttp.Enabled = True
        gbctp.Enabled = False
        gbbctp.Enabled = False
        dgvpp.Rows.Clear()
    End Sub

    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        PhucHoiSua()
        If (Val(txttienphat.Text) <= 0) Then
            MsgBox("Tiền phạt phải lớn hơn 0", MsgBoxStyle.Information, "Thông tin")
        End If
        Dim i As Integer = dgvpp.Rows.Add
        dgvpp.Rows(i).Cells("LyDo").Value = txtlydo.Text
        dgvpp.Rows(i).Cells("TienPhat").Value = Val(txttienphat.Text)
        TinhTongTien()
        btnnhaplai_Click(sender, e)
    End Sub
    Private Sub PhucHoiSua()
        btnsua.Text = "&Sửa"
    End Sub
    Private Sub btnsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click
        If (btnsua.Text = "&Sửa") Then
            editingindex = dgvpp.CurrentRow.Index
            txtlydo.Text = dgvpp.Rows(editingindex).Cells("LyDo").Value
            txttienphat.Text = dgvpp.Rows(editingindex).Cells("TienPhat").Value
            btnsua.Text = "&Lưu"
        Else
            dgvpp.Rows(editingindex).Cells("LyDo").Value = txtlydo.Text
            dgvpp.Rows(editingindex).Cells("TienPhat").Value = txttienphat.Text
            PhucHoiSua()
            btnnhaplai_Click(sender, e)
        End If
        TinhTongTien()
    End Sub

    Private Sub btnxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoa.Click
        PhucHoiSua()
        dgvpp.Rows.Remove(dgvpp.CurrentRow)
        TinhTongTien()
    End Sub
    Private Sub TinhTongTien()
        Dim t As Double = 0
        For Each r As DataGridViewRow In dgvpp.Rows
            t += Val(r.Cells("TienPhat").Value)
        Next
        txttong.Text = t
    End Sub

    Private Sub FrmPhieuPhat_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        If MaDG = "" Then
            txtmadg.ReadOnly = False
            txtmadg.BorderStyle = BorderStyle.Fixed3D
        Else
            txttendg.Text = clsDocGia.ThongTinDG(MaDG).Rows(0)("TenDG")
        End If
        txtmadg.Text = MaDG

        txttennv.Text = clsNhanVien.ThongTinNV(MaNV).Rows(0)("TenNV")
        txtngaylap.Text = Date.Now.ToString("dd/MM/yyyy")
    End Sub

    Private Sub dgvpp_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvpp.CellDoubleClick
        PhucHoiSua()
        btnsua_Click(sender, e)
    End Sub

    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        Close
    End Sub

    Private Sub btnnhaplai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnhaplai.Click
        txtlydo.Text = ""
        txttienphat.Text =""
    End Sub

    Private Sub txtmaphieu_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub txtmadg_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtmadg.Leave
        Dim dt As DataTable = clsDocGia.ThongTinDG(txtmadg.Text)
        If dt.Rows.Count = 1 Then
            txttendg.Text = clsDocGia.ThongTinDG(txtmadg.Text).Rows(0)("TenDG")
        End If
    End Sub
End Class